package org.jeecg.common.modules.dic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.modules.dic.entity.DicCity;
import org.jeecg.common.modules.dic.mapper.DicCityMapper;
import org.jeecg.common.modules.dic.service.IDicCityService;
import org.jeecg.common.modules.dic.vo.CityChildren;
import org.jeecg.common.util.enums.AreaLevelEnum;
import org.springframework.stereotype.Service;
import org.jeecg.common.modules.dic.vo.CityResVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: dic_city
 * @Author: jeecg-boot
 * @Date:   2020-08-15
 * @Version: V1.0
 */
@Service
public class DicCityServiceImpl extends ServiceImpl<DicCityMapper, DicCity> implements IDicCityService {

    private static final int ONE_PARENT_ID = 0;

    @Override
    public List<DicCity> getByLevelAndParentId(AreaLevelEnum areaLevelEnum, Integer parentId){
        if (areaLevelEnum == AreaLevelEnum.ONE){
            parentId = ONE_PARENT_ID;
        }
        LambdaQueryWrapper<DicCity> query = Wrappers.lambdaQuery();
        query.eq(DicCity::getLevel,areaLevelEnum.getCode());
        query.eq(DicCity::getParentId,parentId);
        return list(query);
    }

    @Override
    public List<CityResVo> getCityAll(){
//        LambdaQueryWrapper<DicCity> query = Wrappers.lambdaQuery();
//        query.eq(DicCity::getParentId,1);
//        List<DicCity> city = list(query); //获取省/市
//
//        List<CityReqVo> vos = new ArrayList<>();
//        for (DicCity ct:city) {
//            LambdaQueryWrapper<DicCity> queryWrapper = Wrappers.lambdaQuery();
//            queryWrapper.eq(DicCity::getParentId,ct.getId());
//            CityReqVo vo = new CityReqVo();
//            vo.setId(ct.getId());
//            vo.setName(ct.getName());
//            vo.setLevel(ct.getLevel());
//            vo.setList(list(queryWrapper));
//            vos.add(vo);
//        }

        LambdaQueryWrapper<DicCity> query = Wrappers.lambdaQuery();
        query.eq(DicCity::getParentId,1);
        List<DicCity> city = list(query); //获取省/市

        List<CityResVo> vos = new ArrayList<>();
        for (DicCity ct:city) {
            LambdaQueryWrapper<DicCity> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DicCity::getParentId,ct.getId());
            CityResVo vo = new CityResVo();
            vo.setValue(ct.getId());
            vo.setLabel((ct.getName()));

            List<DicCity> dCityList = list(queryWrapper);
            List cityChildrenList = new ArrayList();
            for(DicCity dicCity:dCityList){
                CityChildren dicChildren = this.initCityChildren(dicCity);
                cityChildrenList.add(dicChildren);
            }
            vo.setChildren(cityChildrenList);

            vos.add(vo);
        }
        return vos;
    }

    /**
     * 处理城市子级
     * @param dicCity
     * @return
     */
    public CityChildren initCityChildren(DicCity dicCity){
        CityChildren cityChildren = new CityChildren();
        cityChildren.setLabel(dicCity.getName());
        cityChildren.setValue(dicCity.getId());
        return cityChildren;
    }


    @Override
    public List<DicCity> getFirstRegion(){
        LambdaQueryWrapper<DicCity> query = Wrappers.lambdaQuery();
        query.eq(DicCity::getParentId,1);
        return list(query);
    }

    @Override
    public List<DicCity> getSecondRegion(Integer id){
        LambdaQueryWrapper<DicCity> query = Wrappers.lambdaQuery();
        query.eq(DicCity::getParentId,id);
        return list(query);
    }
}
